


import ConfigParser
import os

class Config:
  def __init__(self,file):
    self.conf = ConfigParser.RawConfigParser()
    filename = 'conf\\%s' %(file)
    path = os.getcwd() #path+"\\"+
    #path = os.path.abspath("..")
    p = ".."
    while not os.path.exists(path + "\\"+filename):      
      path = os.path.abspath(p)
      p = p+"\\.."      
      #print path
      
      
    try:
      self.cfgpath = path+"\\"+filename   
      #print self.cfgpath
      self.f = open(self.cfgpath,"r")
      self.conf.readfp(self.f)
    except:
      print "fail to open " + filename


  def getOption(self, section, option):
    if self.conf.has_option( section,option):
      return self.conf.get(section,option)
    else:
      print "could not find "+ option

  def setOption(self,section, option,value):
    if self.conf.has_option( section,option):
      return 0
    else:
      self.conf.set( section, option, value)
      newc = open(self.cfgpath,"w")
      print newc 
      self.conf.write(newc)
      print "write to conf"
      
  def options(self,section):      
    return self.conf.options(section)

  def removeOption():
    pass



def test():
  cfg = Config('project.cfg')
  print cfg.getOption('project','name')
  print cfg.getOption('project','basedir')
  print cfg.getOption('database','username')
  #cfg.setOption('option2','1')
if __name__ == "__main__":
  test()
